﻿// Upgrade NOTE: replaced 'defined U_ON' with 'defined (U_ON)'
// Upgrade NOTE: replaced 'defined V_ON' with 'defined (V_ON)'

Shader "Memo/UVAlphaFade"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        [Toggle(U_ON)]_IsX("Is U OffSet?",int) = 0
        [Toggle(V_ON)]_IsY("Is V OffSet?",int) = 0
        [Toggle(U_INVERSE)]_IsXInverse("Is U Inverse?",int) = 0
        [Toggle(V_INVERSE)]_IsYInverse("Is V Inverse?",int) = 0
        _AlphaOffSetX("Alpha Offset U",float) = 0
        _AlphaOffSetY("Alpha Offset V",float) = 0
        _AlphaFadeRange("Alpha Fade Range",Range(0,1)) = 0.1

    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue" = "Transparent" }
        LOD 100

        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #pragma multi_compile _ U_ON
            #pragma multi_compile _ V_ON
            #pragma multi_compile _ U_INVERSE
            #pragma multi_compile _ V_INVERSE

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _AlphaOffSetX,_AlphaOffSetY;
            float _AlphaFadeRange;
            float _IsX,_IsY;
            float _IsXInverse,_IsYInverse;


            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                 
                //Alpha
                //float alphaFadeRange = step(_AlphaOffSetX,_AlphaFadeRange)*_AlphaFadeRange;


                float alphaX = 1;
                float alphaY = 1;
                #if defined (U_ON)
                alphaX = smoothstep(_AlphaOffSetX,_AlphaOffSetX+_AlphaFadeRange,i.uv.x);
                #endif

                #if defined (V_ON)
                alphaY = smoothstep(_AlphaOffSetY,_AlphaOffSetY+_AlphaFadeRange,i.uv.y);                
                #endif

                #if defined (U_INVERSE)
                alphaX = 1 - alphaX;
                #endif

                #if defined(V_INVERSE)
                alphaY = 1 - alphaY;
                #endif

                float alpha = alphaX * alphaY;
                col.a = alpha;
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}
